//
//  TMHomeViewModel.m
//  国学培训用户端
//
//  Created by Tao Li on 2018/8/9.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "TMHomeViewModel.h"
#import "TMBarnerModel.h"
#import "TMRecommendModel.h"
#import "TMCourseDetailModel.h"
#import "TMCommentModel.h"
#import "TMShopCourseModel.h"
#import "TMSearchModel.h"
#import "TMHomePageController.h"
@interface TMHomeViewModel()
@property (nonatomic, strong) NSMutableArray *allList;
@property (nonatomic, strong) NSMutableArray *teacherList;
@end
@implementation TMHomeViewModel
- (NSMutableArray *)allList {
    if (_allList ==nil) {
        _allList = [NSMutableArray array];
    }return _allList;
}
- (NSMutableArray *)teacherList {
    if (_teacherList ==nil) {
        _teacherList = [NSMutableArray array];
    }return _teacherList;
}
#pragma --mark 首页数据
- (void)getHomePageDataWithParam:(NSString *)dic Success:(CompleteBlock)block Failed:(FailureBlock)failre{
    NSMutableDictionary *param = [TMUserManager paramDisplay];
    [param setValue:dic forKey:@"degree"];
    [[TMDataRequest defaultRequest] requestWithPath:TMAPI_GetHomePageData parameters:param method:1 prepareExecute:^{
        
    } success:^(NSURLSessionDataTask *task, id responseObject) {
        NSMutableArray *list1 = [NSMutableArray array];
        self.teacherList = NSMutableArray.new;
        for (NSDictionary*dic in responseObject[@"banner"]) {
            TMBarnerModel *model = [TMBarnerModel yy_modelWithJSON:dic];
            [list1 addObject:model];
        }
        [self.teacherList addObject:list1];
        
        NSMutableArray *list2 = [NSMutableArray array];
        for (NSDictionary*dic in responseObject[@"list"]) {
            TMRecommendModel *models = [TMRecommendModel yy_modelWithJSON:dic];
            [list2 addObject:models];
        }
        [self.teacherList addObject:list2];
        //当地机构
        NSMutableArray *list3 = [NSMutableArray array];
        for (NSDictionary*dic in responseObject[@"data"]) {
            TMRecommendModel *models = [TMRecommendModel yy_modelWithJSON:dic];
            [list3 addObject:models];
        }
        [self.teacherList addObject:list3];
        //培训分类
        NSMutableArray *list4 = [NSMutableArray array];
        for (NSDictionary*dic in responseObject[@"type"]) {
            TMRecommendModel *models = [TMRecommendModel yy_modelWithJSON:dic];
            [list4 addObject:models];
        }
        [self.teacherList addObject:list4];
        block(self.teacherList,@"1");
    } error:^(NSInteger errorCode, NSString *errorMessage) {
        failre(errorMessage);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        failre(@"1");
    }];
}
#pragma --mark 首页item 分类
- (void)getTypeWithParam:(NSInteger)fid AndPage:(NSInteger)page Success:(CompleteBlock)block Failed:(FailureBlock)failre{
    if (page == 1) {
         [self.teacherList removeAllObjects];
    }
    NSMutableDictionary *param = [TMUserManager paramDisplay];
    [param setValue:@(fid) forKey:@"id"];
    [param setValue:@(page) forKey:@"page"];
    UINavigationController * nav =  UIApplication.sharedApplication.delegate.window.rootViewController.childViewControllers.firstObject;
    TMHomePageController * vc = (id)nav.childViewControllers.firstObject;
    NSString * degree = [NSString stringWithFormat:@"[%@,%@]",vc.latitude,vc.longitude];
    [param setObject:degree forKey:@"degree"];
    
    [[TMDataRequest defaultRequest] requestWithPath:TMAPI_GetTypeData parameters:param method:1 prepareExecute:^{
        
    } success:^(NSURLSessionDataTask *task, id responseObject) {
        NSMutableArray *list1 = [NSMutableArray array];
        for (NSDictionary *dic in responseObject[@"list"]) {
            TMCourseDetailModel *model = [TMCourseDetailModel yy_modelWithJSON:dic];
            [list1 addObject:model];
        }
        [self.teacherList addObjectsFromArray:list1];
        block(self.teacherList,responseObject[@"total"]);
    } error:^(NSInteger errorCode, NSString *errorMessage) {
         failre(errorMessage);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
         failre(@"1");
    }];
}
#pragma --mark 推荐专区
- (void)getRecommendDetailParam:(NSInteger)fid Page:(NSInteger)page Success:(CompleteBlock)block Failed:(FailureBlock)failre{
    if (page == 1) {
        [self.teacherList removeAllObjects];
    }
    NSMutableDictionary *param = [TMUserManager paramDisplay];
    [param setValue:@(page) forKey:@"page"];
    [param setValue:@(fid) forKey:@"id"];
    [[TMDataRequest defaultRequest] requestWithPath:TMAPI_GetRecommendDetail parameters:param method:1 prepareExecute:^{
        
    } success:^(NSURLSessionDataTask *task, id responseObject) {
        TMCourseDetailModel *model = [TMCourseDetailModel yy_modelWithJSON:responseObject[@"list"]];
        model.average = [responseObject[@"data"][@"average"]integerValue];
        model.total = [responseObject[@"data"][@"total"]integerValue];
        model.totalPage = [responseObject[@"totalPage"] integerValue];
        model.rank = [responseObject[@"rank"] integerValue];
        //tips
        NSMutableArray *arr = [NSMutableArray array];
        for (NSDictionary *dic in responseObject[@"tip"]) {
            TMCommentModel *modess = [TMCommentModel yy_modelWithJSON:dic];
            [arr addObject:modess];
        }
        [self.teacherList addObjectsFromArray:arr];
        block(model,self.teacherList);
    } error:^(NSInteger errorCode, NSString *errorMessage) {
        failre(errorMessage);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        failre(@"1");
    }];
}
#pragma --mark 加入购物车
- (void)addShoppingCartParam:(NSInteger)fid Success:(SuccessBlock)block Failed:(FailureBlock)failre{
    NSMutableDictionary *param = [TMUserManager paramDisplay];
    [param setValue:@(fid) forKey:@"cid"];
    [[TMDataRequest defaultRequest] requestWithPath:TMAPI_AddShoppingCart parameters:param method:1 prepareExecute:^{
        
    } success:^(NSURLSessionDataTask *task, id responseObject) {
        [TMProgressHUD showSuccessWithStyleDarkStatus:responseObject[@"msg"]];
        block(responseObject);
    } error:^(NSInteger errorCode, NSString *errorMessage) {
        failre(errorMessage);
        [TMProgressHUD showInfoWithStyleDarkStatus:errorMessage];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        failre(@"1");
    }];
}

#pragma --mark 店铺首页
- (void)getShopHomeDataWithShop:(NSInteger)sid Success:(CompleteBlock)success AndFiled:(FailureBlock)faild{
    [self.teacherList removeAllObjects];
    NSMutableDictionary *dic = [TMUserManager paramDisplay];
    [dic setValue:@(sid) forKey:@"id"];
    [[TMDataRequest defaultRequest] requestWithPath:TMAPI_GetShopDetail parameters:dic method:0 prepareExecute:^{
        
    } success:^(NSURLSessionDataTask *task, id responseObject) {
        NSMutableArray *arr = [NSMutableArray array];
        for (NSDictionary *dic in responseObject[@"course"]) {
            TMShopCourseModel *cmodel = [TMShopCourseModel yy_modelWithJSON:dic];
            [arr addObject:cmodel];
        }
        [self.teacherList addObject:arr];
        
        TMShopListModel *model = [TMShopListModel yy_modelWithJSON:responseObject[@"list"]];
        [self.teacherList addObject:model];
        
        TMShopProveModel *pmodel = [TMShopProveModel yy_modelWithJSON:responseObject[@"list"]];
        [self.teacherList addObject:pmodel];
        
        NSMutableArray *list = [NSMutableArray array];
        for (NSDictionary *dicc in responseObject[@"teacher"]) {
            TMShopTeacherModel *models = [TMShopTeacherModel yy_modelWithJSON:dicc];
            [list addObject:models];
        }
        [self.teacherList addObject:list];
        NSString *hascollection = responseObject[@"list"][@"coll"];
        [self.teacherList addObject:hascollection];
        success(self.teacherList, @"1");
    } error:^(NSInteger errorCode, NSString *errorMessage) {
        [TMProgressHUD showErrorWithStyleDarkStatus:errorMessage];
        faild(errorMessage);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        faild(@"1");
    }];
}
#pragma --mark 店铺收藏
-(void)getShopCollectionWith:(NSInteger)cid Success:(SuccessBlock)success failed:(FailureBlock)failed{
    NSMutableDictionary *dic = [TMUserManager paramDisplay];
    [dic setValue:@(cid) forKey:@"prove_id"];
    [[TMDataRequest defaultRequest] requestWithPath:TMAPI_GetShopCollection parameters:dic method:1 prepareExecute:^{
        
    } success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"%@", responseObject);
        [TMProgressHUD showSuccessWithStyleDarkStatus:responseObject[@"msg"]];
        success(responseObject[@"msg"]);
    } error:^(NSInteger errorCode, NSString *errorMessage) {
        failed(errorMessage);
        [TMProgressHUD showErrorWithStyleDarkStatus:errorMessage];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        failed(@"1");
        [TMProgressHUD showErrorWithStyleDarkStatus:@"网络错误！"];
    }];
}
#pragma --mark 教师数据
-(void)getSearchDataWithKwords:(NSDictionary*)param Success:(CompleteBlock)success failed:(FailureBlock)failed{
    if ([param[@"page"] integerValue] == 1) {
        [self.teacherList removeAllObjects];
    }
    NSMutableDictionary *dic = [TMUserManager paramDisplay];
    [dic addEntriesFromDictionary:param];
    //[dic setValue:@(cid) forKey:@"prove_id"];
    [[TMDataRequest defaultRequest] requestWithPath:TMAPI_GetSearchData parameters:dic method:1 prepareExecute:^{
        
    } success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"%@", responseObject);
        NSMutableArray *arr = [NSMutableArray array];
        for (NSDictionary *dic in responseObject[@"list"]) {
            TMSearchModel *model = [TMSearchModel yy_modelWithJSON:dic];
            model.mold = [responseObject[@"mold"] integerValue];
            [arr addObject:model];
        }
        [self.teacherList addObjectsFromArray:arr];
//        [TMProgressHUD showSuccessWithStyleDarkStatus:responseObject[@"msg"]];
        success(self.teacherList,responseObject[@"total"]);
    } error:^(NSInteger errorCode, NSString *errorMessage) {
        failed(errorMessage);
        [TMProgressHUD showErrorWithStyleDarkStatus:errorMessage];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        failed(@"1");
        [TMProgressHUD showErrorWithStyleDarkStatus:@"网络错误！"];
    }];
}
#pragma --mark 聊天信息
-(void)getChatInformationWith:(NSInteger)cid Success:(SuccessBlock)success failed:(FailureBlock)failed{
    NSMutableDictionary *dic = [TMUserManager paramDisplay];
    [dic setValue:@(cid) forKey:@"id"];
    //[dic setValue:@(cid) forKey:@"prove_id"];
    [[TMDataRequest defaultRequest] requestWithPath:TMAPI_GetChatInformation parameters:dic method:1 prepareExecute:^{
        
    } success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"%@", responseObject);
//        NSMutableArray *arr = [NSMutableArray array];
//        for (NSDictionary *dic in responseObject[@"list"]) {
//            TMSearchModel *model = [TMSearchModel yy_modelWithJSON:dic];
//            [arr addObject:model];
//        }
//        [self.teacherList addObjectsFromArray:arr];
        [TMProgressHUD showSuccessWithStyleDarkStatus:responseObject[@"msg"]];
        success(responseObject);
    } error:^(NSInteger errorCode, NSString *errorMessage) {
        failed(errorMessage);
        [TMProgressHUD showErrorWithStyleDarkStatus:errorMessage];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        failed(@"1");
        [TMProgressHUD showErrorWithStyleDarkStatus:@"网络错误！"];
    }];
}
@end
